﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ProgNetComponents.Ini;
using System.Reflection;
using ProgNetComponents.Logs;

namespace Wydatki
{
    static class Program
    {
        static class Argumenty
        {
            public const string Debug = "-debug";
            
        }

        private static string[] Args;

        public static bool IsExit
        {
            get;
            set;
        }
        public static bool IsDebug
        {
            get { return IsArgPresent(Argumenty.Debug); }
        }
        public static bool IsArgPresent(string arg)
        {
            return Args != null && Args.Contains(arg);
        }
       
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            Args = args;

            ProgNetComponents.Logs.Logger.CurrentLogType = ProgNetComponents.Logs.LogType.Error;
            if (IsDebug)
                ProgNetComponents.Logs.Logger.CurrentLogType = ProgNetComponents.Logs.LogType.All;
            ProgNetComponents.Logs.LoggerProperties p = new ProgNetComponents.Logs.LoggerProperties();
           
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pl-PL");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("pl-PL");

            p.AdditionalStackTraceMaxLines = 5;
            p.FileName = Application.StartupPath + "\\ErrorLog.txt";
            ProgNetComponents.Utils.DefaultBool debugBool = (IsDebug ? ProgNetComponents.Utils.DefaultBool.True : ProgNetComponents.Utils.DefaultBool.False);
            p.LogAdditionalStackTrace = debugBool;
            p.LogDate = ProgNetComponents.Utils.DefaultBool.True;
            p.LogExceptionName = ProgNetComponents.Utils.DefaultBool.True;
            p.LogExceptionProperties = debugBool;
            p.LogInnerExceptios = debugBool;
            p.LogLogType = ProgNetComponents.Utils.DefaultBool.True;
            p.LogStackTrace = ProgNetComponents.Utils.DefaultBool.True;
            p.LogTime = ProgNetComponents.Utils.DefaultBool.True;
            p.LogType = (IsDebug ? ProgNetComponents.Logs.LogType.All : ProgNetComponents.Logs.LogType.Error);
            p.StackTraceMaxLines = 11;
            p.LogExceptionMessage = ProgNetComponents.Utils.DefaultBool.True;
            Logger.InitLoggerProperties("", p);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Tools.Globals.ShowExceptionDetailsForm = true;

            try
            {
                FieldInfo fi = typeof(DevExpress.Utils.About.Utility).GetSingleField("staticAboutShown", BindingFlags.NonPublic | BindingFlags.Static);
                if (fi != null)
                    fi.SetValue(null, true);
            }
            catch { }
            ProgNetComponentsDx.Forms.FormSkinChooser.RegisterSkins();
            Properties.Resources.Money2.SetAsDefaultFormIcon();

            ProgNetComponentsDx.SingleAppRunnerDx single = new ProgNetComponentsDx.SingleAppRunnerDx();
            
            single.Run(() => { return new Form1(); }, () => { return new Init.InitForm(); });
        }
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = null;
            if (e.ExceptionObject != null && e.ExceptionObject is Exception)
            {
                ex = e.ExceptionObject as Exception;
            }
            else
            {
                ex = new Exception("Unhandeled exception: " + e.ExceptionObject.AsString());
            }
            Tools.Globals.HandleException(ex, "== CurrentDomain.UnhandledException ==");
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Tools.Globals.HandleException(e.Exception, "== Application.ThreadException ==");
        }
        
    }
}
